import { HttpClient, HttpParams } from '@angular/common/http';
import { Response } from "../../models/response";
import { Injectable } from '@angular/core';
import { ToastController } from "ionic-angular";
import { Subject } from 'rxjs/Subject';
import { debounceTime, switchMap } from "rxjs/operators";

/**
 * Api is a generic REST Api handler. Set your API url first.
 */
@Injectable()
export class Api {
  // url: string = 'https://example.com/api/v1';

  //public static IP_PORT = 'http://192.168.0.23:2002';


  //  public static IP_PORT = 'http://192.168.0.143:1001';
   public static IP_PORT = 'http://39.104.121.207:1022';
  // public static IP_PORT = 'http://139.196.23.86:2022';

  public static PROJECT_PACKAGE = '/Mobile';
  url = Api.IP_PORT + Api.PROJECT_PACKAGE;

  res;
  private sub = new Subject();

  constructor(public http: HttpClient, public toastCtrl: ToastController) {
    // this.res = this.sub.pipe(
    //   debounceTime(2000),
    //   switchMap((arr)=>this.get(...arr))
    // );
  }

  get(endpoint: string, params?: any, reqOpts?: any) {
    if (!reqOpts) {
      reqOpts = {
        params: new HttpParams()
      };
    }

    // Support easy query params for GET requests
    if (params) {
      reqOpts.params = new HttpParams();
      for (let k in params) {
        reqOpts.params = reqOpts.params.set(k, params[k]);
      }
    }

    return this.http.get(this.url + '/' + endpoint, reqOpts);
  }

  post(endpoint: string, body: any, reqOpts?: any) {
    return this.http.post(this.url + '/' + endpoint, body, reqOpts);
  }

  gets(p) {
    return this.get("EditPwd", p);
  }
  getList(p) {
    return this.get("userlogin", p);
  }
  questionnaireValidation(p) {
    return this.get("GetQueInfo", p);
  }
  notDownloadList(p) {
    return this.get("ListQuestionnaireByUserId", p);
  }

  questionnaireAnswers(p) {
    // this.sub.next(["LoadQueAndTopics",p]);
    // return this.res;
    return this.get("LoadQueAndTopics", p);
  }
  OverList(p) {
    return this.get("ListEndQuestionnaireByUserId", p);
  }
   addMobileAnswer(p) {
    // return this.post("AddMobileAnswer","",p);
    // return this.http.post(this.url + '/' + "AddMobileAnswer", "", p);
    return this.postnew<any>(this.url + '/' + "AddMobileAnswer", p);
  }
  IsGengAsQue(p) {
    return this.get("IsGengAsQue", p);
  }
  GetRegions() {
    return this.get("GetRegions");
  }
  Testwl(p) {
    return this.get("Testwl",p);
  }

  postnew<T>(url: string, body: any): Promise<T> {
    return new Promise<T>((resolve, reject) => {
      this.http.post<any>(url, body).subscribe(value => {
        resolve(<T>(value));
      }, error => {
        // this._showErrorMsg(error, reject);
      });
    }).catch(error => {
      if (error.statusText && (error.statusText === "Unknown Error" || error.statusText === "Internal Server Error")) {
        // this._message.error('服务器错误，请联系管理员!');
      }
      throw new Error("未知错误");
    });
  }
  _post<I, O>(url: string, input: I): Promise<O> {
    return new Promise((resolve, reject) => {
      this.http.post(this.url + '/' + url, input).subscribe(res => {
        let response = (<Response>res);
        if (response.success) {
          resolve(<O>response.result);
        } else {
          let toast = this.toastCtrl.create({
            message: 'test',
            duration: 3000,
            position: 'top'
          });
          toast.present();
          reject(false);
        }
      });
    });
  }
}

